package com.graduation.smartparkingplatform.Controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Distinguish;
import com.graduation.smartparkingplatform.Pojo.Visitor;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.ComponentService;
import com.graduation.smartparkingplatform.Utils.IPAddressUtils;
import com.graduation.smartparkingplatform.Utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/component")
public class ComponentController {

  @Autowired ComponentService componentService;
  @Autowired RestTemplate restTemplate;

  @GetMapping("/weather")
  public Response getWeatherInfo(HttpServletRequest request) {
    String ipAddress = IpUtil.getIpAddr(request);
    // 解析IP地址城市
    IPAddressUtils ip = new IPAddressUtils();
    ip.init();
    // 获取ip城市
    String city = ip.getIPLocation(ipAddress).getCountry();
    String str1 = city.substring(0, city.indexOf("省"));
    String str2 = city.substring(str1.length() + 1, city.length() - 1);
    String apiURL = "http://wthrcdn.etouch.cn/weather_mini?city=" + str2;
    ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);
    if (responseEntity.getStatusCodeValue() == 200) {
      String result = responseEntity.getBody();
      JSONObject jsonObject = JSONObject.parseObject(result);
      Map<String, Object> map = (Map<String, Object>) jsonObject; //    //json对象转Map
      return ResponseUtils.buildSuccess("200", "获取天气数据成功", map);
    } else {
      return ResponseUtils.buildError("获取天气数据失败");
    }
  }

  // 获取系统访客
  @GetMapping("/visitor")
  public Response getLog(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "20") Integer count) {
    try {
      PageInfo<Visitor> logList = componentService.getLog(page, count);
      return ResponseUtils.buildSuccess(logList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取访客设备信息
  @GetMapping("/device")
  public Response getLogDevice() {
    try {
      Map<String, Object> result = componentService.getLogDevice();
      return ResponseUtils.buildSuccess(result);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 数据统计信息
  @GetMapping("/statistics")
  public Response getStatistics() {
    try {
      Map<String, Object> result = componentService.getStatistics();
      return ResponseUtils.buildSuccess(result);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取车辆进出记录
  @GetMapping("/record")
  public Response getRecord(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Distinguish> result = componentService.getRecord(page, count);
      return ResponseUtils.buildSuccess(result);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取收入统计7日
  @GetMapping("/paymentByWeek")
  public Response paymentByWeek() {
    try {
      List<Object> result = componentService.weekPayment();
      return ResponseUtils.buildSuccess(result);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取收入统计30日
  @GetMapping("/paymentByMonth")
  public Response paymentByMonth() {
    try {
      List<Object> result = componentService.monthPayment();
      return ResponseUtils.buildSuccess(result);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }
}
